import React from 'react';
import address from '../../assets/css/user/address.module.css'

export default class AddressComponent extends React.Component{
    constructor(){
        super();
        this.state={
            list:[]
        }
    }
    componentWillMount() {
        this.getData()
    }
    getData(){
        fetch('http://vueshop.glbuys.com/api/user/address/index?uid='+localStorage['uid']+'&token=1ec949a15fb709370f')
            .then((res)=>res.json())
            .then(res=>{
                if(res.code===200){
                    this.setState({list:res.data})
                }
            })
    }
    goBack(){
        this.props.history.goBack()
    }
    add(){
        this.props.history.push('/user/add')
    }
    goAdd(path){
        this.props.history.push(path)
    }

    render() {
        return(
            <div className={address.boxAddress}>
                <div>
                    <img src={require("../../assets/images/home/goods/back.png")} onClick={this.goBack.bind(this)} alt=""/>
                    <span>收货管理</span>
                </div>
                <div style={{paddingTop:'60px'}}>
                    {this.state.list.map((item,index)=>{
                        return(
                            <div key={index} onClick={this.goAdd.bind(this,'/user/mod?aid='+item.aid)} className={address.boxActive}>
                                <div>
                                    <p>{item.name}&nbsp;&nbsp;{item.cellphone}</p>
                                    <p><span className={item.isdefault === '1'?address.show:address.hide}>[默认]</span>{item.province}{item.city}{item.area}{item.address}</p>
                                </div>
                                <img src="images/common/right_arrow.png" alt=""/>
                            </div>
                        )
                    })}
                </div>
                <button onClick={this.add.bind(this)}>+添加新地址</button>
            </div>
        )
    }
}